$(function(){
    $(".black").on("click",function(){
        debugger;
        var id=$(this).attr("status");
        ShowTipBox();
        $(".a-confirm").on("click",function(){
            ajax("post","/tree/delete-tree",{id:id},function success(data){
                if(data.code==200){
                    $(".a-div").hide();
                    layer.msg(data.msg);
                    setTimeout(function(){
                        location.reload();
                    },800)
                }
            },function error(data){
                layer.msg(data.msg)
            })
        });
        $(".a-cancel").on("click",function(){
            $(".a-div").hide();
        });
    });
    $(".father").click(function(e){
        var e=e||event;
        var nextAll=$(this).nextAll();
        var status=$(this).attr("status");
        for(var i=0;i<nextAll.length;i++){
            if(nextAll[i].className!="father"){
                nextAll[i].style.display=status=='on'?'none':'table-row';
                $(this).attr("status",status=='on'?'off':'on');
            }else{
                return false;
            }
        }
    });
    $(".firstChild").click(function(){
        var firstNext=$(this).nextAll(),
            status=$(this).attr("status");
        for(var i = 0,len = firstNext.length;i < len ; i++)
        {
            if(firstNext[i].className != 'secondChild'){
                return false;
            }
            firstNext[i].style.display= status == 'on' ? 'none' : 'table-row';
            $(this).attr("status",status == 'on' ? 'off' : 'on');
        }
        event.stopImmediatePropagation();
    });

    // uploaderExcel.eventBind();

    //上传excel文件
    var uploaderExcel = {
        'data':{
            'token':'',
            'uploadDomain':'',
            'domain':''
        },
        //  上传成功后的回调.
        'complete':function(res){
            // 这里只需要res.key
            var url="http://"+uploaderExcel.data.domain +"/"+ res.key;
            console.log(url);
            // $('#shop_logo').attr('src',url);
            $.ajax({
                'type':'POST',
                'url':'/tree/import-menu',
                'data':{
                    excel_file:res.key
                },
                'dataType':'json',
                'success':function(data)
                {
                    console.dir(data);
                    layer.closeAll();
                    layer.alert(data.msg);
                }

            });
        },
        // 上传时的错误.
        'error':function () {
            console.dir(arguments);
        },
        // 分片上传时的进度展示功能在此书写.
        'next':function (response) {
            // var chunks = response.chunks || [];
            // var total = response.total;
        },
        // 初始化获取token.
        'init':function(){
            $.ajax({
                'url':'/upload/get-token',
                'type':'POST',
                'dataType':'json',
                'success':function(data){
                    if(data.code != 200)
                    {
                        layer.alert(data.msg);
                        return false;
                    }
                    uploaderExcel.data.token = data.data.token;      // token
                    uploaderExcel.data.domain = data.data.domain;    // 显示域名
                    // uploader.data.uploadDomain = 'idv093d.qiniudns.com';    // 上传域名
                    // console.log(uploader.data.domain)
                }
            });
        },
        'eventBind':function(){
            this.init();
            $('#tree-excel').on('change',function () {
                var token = uploaderExcel.data.token;
                var config = {
                    useCdnDomain: true,
                    disableStatisticsReport: false,
                    region: qiniu.region.z0
                };

                var putExtra = {
                    fname: "",
                    params: {},
                    mimeType: ["application/wps-office.xlsx", "application/wps-office.xls"], // 限制文件类型.
                };

                var file = this.files[0];

                var observable;

                if (file) {
                    var files=$("#tree-excel")[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                    var key1=file.name;
                    var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                    putExtra.params["x:name"] = key.split(".")[0];
                    // 配置完成的数组.
                    var subObject = {
                        next: uploaderExcel.next,
                        error: uploaderExcel.error,
                        complete: uploaderExcel.complete
                    };
                    var subscription;
                    // 调用sdk上传接口获得相应的observable，控制上传和暂停
                    observable = qiniu.upload(files, key, token, putExtra, config);
                    subscription = observable.subscribe(subObject);
                    // subscription.unsubscribe();  // 取消上传.
                }
            });
        }
    };
    // $(document).ready(function(){
    //     uploaderExcel.eventBind();
    // });
    $('#tree-excel').on('change',function (event) {
        // var file = $('#excel').serialize();
        var fileList = event.target.files;
        if(!fileList || fileList.length == 0){
            return;
        }
        var formData = new FormData();
        for(var i=0,len=fileList.length;i<len;i++){
            formData.append('excel',fileList[i]);
        }
        $.ajax({
            'type':'POST',
            'url':'/tree/import-menu',
            'data':formData,
            'dataType':'json',
            'contentType': false,
            'cache': false,
            'processData': false,
            'success':function(data)
            {
                console.dir(data);
                layer.closeAll();
                layer.alert(data.msg);
            }

        });
    })

});